/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Connection.java
 *
 * Created on 08.05.2011, 12:13:08
 */
package siedleronlineproxy.forms.connection;

import java.io.File;
import java.io.FileInputStream;
import siedleronlineproxy.LookAndFeel;
import siedleronlineproxy.PropertyManager;
import siedleronlineproxy.TheAllUpdatingThread;
import siedleronlineproxy.debug.DebugLog;
import siedleronlineproxy.network.PreferedAMFDeserializer;
import siedleronlineproxy.network.Proxy;
import siedleronlineproxy.registry.BuildingRegistry;

/**
 *
 * @author nspecht
 */
public class Connection extends javax.swing.JPanel {

    private static Connection instance;

    public static Connection getInstance() {
        return Connection.instance;
    }
    Proxy proxy = new Proxy();
    TheAllUpdatingThread taut = new TheAllUpdatingThread();

    /** Creates new form Connection */
    public Connection() {
        Connection.instance = this;
        initComponents();
        LookAndFeel.getInstance().fillComboBox(this.jComboBox1);
        PropertyManager.LoadComboBox(this.jComboBox1);
        PropertyManager.LoadTextField(this.jTextField1);
        PropertyManager.LoadSpinner(this.jSpinner1);
        PropertyManager.LoadSpinner(this.jSpinner2);
        PropertyManager.LoadCheckbox(this.jCheckBox1);
        PropertyManager.LoadCheckbox(this.jCheckBox2);
        PropertyManager.LoadCheckbox(this.jCheckBox3);
        PropertyManager.LoadCheckbox(this.jCheckBox4);
        PropertyManager.LoadCheckbox(this.jCheckBox5);
        PropertyManager.LoadCheckbox(this.jCheckBox6);
        PropertyManager.LoadCheckbox(this.jCheckBox7);
        PropertyManager.LoadCheckbox(this.jCheckBox8);
        PropertyManager.LoadCheckbox(this.jCheckBox9);
        PropertyManager.LoadCheckbox(this.jCheckBox10);
        this.taut.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jCheckBox8 = new javax.swing.JCheckBox();
        jCheckBox9 = new javax.swing.JCheckBox();
        jCheckBox10 = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        jSeparator1 = new javax.swing.JSeparator();
        jCheckBox1 = new javax.swing.JCheckBox();
        jSeparator2 = new javax.swing.JSeparator();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jButton3 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(3000, 3000));
        setName("Form"); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridBagLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(siedleronlineproxy.SiedlerOnlineProxyApp.class).getContext().getResourceMap(Connection.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jButton1, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(100, 170));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jCheckBox2.setText(resourceMap.getString("DEBUG_ENABLED.text")); // NOI18N
        jCheckBox2.setName("DEBUG_ENABLED"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox2, gridBagConstraints);

        jCheckBox3.setText(resourceMap.getString("DEBUG_PRINT_NOTICE.text")); // NOI18N
        jCheckBox3.setName("DEBUG_PRINT_NOTICE"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox3, gridBagConstraints);

        jCheckBox4.setText(resourceMap.getString("DEBUG_PRINT_SUCCESS.text")); // NOI18N
        jCheckBox4.setName("DEBUG_PRINT_SUCCESS"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox4, gridBagConstraints);

        jCheckBox5.setText(resourceMap.getString("DEBUG_PRINT_WARNING.text")); // NOI18N
        jCheckBox5.setName("DEBUG_PRINT_WARNING"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox5, gridBagConstraints);

        jCheckBox6.setText(resourceMap.getString("DEBUG_PRINT_ERROR.text")); // NOI18N
        jCheckBox6.setName("DEBUG_PRINT_ERROR"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox6, gridBagConstraints);

        jCheckBox7.setText(resourceMap.getString("DEBUG_PRINT_EXCEPTION.text")); // NOI18N
        jCheckBox7.setName("DEBUG_PRINT_EXCEPTION"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox7, gridBagConstraints);

        jCheckBox8.setText(resourceMap.getString("DEBUG_PRINT_METHODCALL.text")); // NOI18N
        jCheckBox8.setName("DEBUG_PRINT_METHODCALL"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox8, gridBagConstraints);

        jCheckBox9.setText(resourceMap.getString("DEBUG_WRITING.text")); // NOI18N
        jCheckBox9.setName("DEBUG_WRITING"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox9, gridBagConstraints);

        jCheckBox10.setText(resourceMap.getString("DEBUG_WRITING_CONTINOUS.text")); // NOI18N
        jCheckBox10.setToolTipText(resourceMap.getString("DEBUG_WRITING_CONTINOUS.toolTipText")); // NOI18N
        jCheckBox10.setName("DEBUG_WRITING_CONTINOUS"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        jPanel1.add(jCheckBox10, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        jPanel2.add(jPanel1, gridBagConstraints);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jButton2, gridBagConstraints);

        jTextField1.setText(resourceMap.getString("proxyname.text")); // NOI18N
        jTextField1.setName("proxyname"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        jPanel2.add(jTextField1, gridBagConstraints);

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(1234, 1, 65000, 1));
        jSpinner1.setName("solpport"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jSpinner1, gridBagConstraints);

        jSpinner2.setModel(new javax.swing.SpinnerNumberModel(1, 1, 65000, 1));
        jSpinner2.setName("proxyport"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jSpinner2, gridBagConstraints);

        jSeparator1.setName("jSeparator1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jSeparator1, gridBagConstraints);

        jCheckBox1.setText(resourceMap.getString("useproxy.text")); // NOI18N
        jCheckBox1.setName("useproxy"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jCheckBox1, gridBagConstraints);

        jSeparator2.setName("jSeparator2"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jSeparator2, gridBagConstraints);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("theme"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        jPanel2.add(jComboBox1, gridBagConstraints);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        jPanel2.add(jLabel1, gridBagConstraints);

        jSeparator3.setName("jSeparator3"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jSeparator3, gridBagConstraints);

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setOpaque(true);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(jButton3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel2, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (this.proxy.connected) {
            this.proxy.disconnect();
            taut.goToSleep();
        } else {
            DebugLog.getInstance().reset();
            this.proxy = new Proxy();
            this.proxy.start();
            taut.wakeUp();
        }
        this.updateGUI();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        PropertyManager.SaveTextField(this.jTextField1);
        PropertyManager.SaveSpinner(this.jSpinner1);
        PropertyManager.SaveSpinner(this.jSpinner2);
        PropertyManager.SaveCheckbox(this.jCheckBox1);
        PropertyManager.SaveCheckbox(this.jCheckBox2);
        PropertyManager.SaveCheckbox(this.jCheckBox3);
        PropertyManager.SaveCheckbox(this.jCheckBox4);
        PropertyManager.SaveCheckbox(this.jCheckBox5);
        PropertyManager.SaveCheckbox(this.jCheckBox6);
        PropertyManager.SaveCheckbox(this.jCheckBox7);
        PropertyManager.SaveCheckbox(this.jCheckBox8);
        PropertyManager.SaveCheckbox(this.jCheckBox9);
        PropertyManager.SaveCheckbox(this.jCheckBox10);
        PropertyManager.SaveComboBox(this.jComboBox1);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {

            this.taut.wakeUp();
            File f = new File("/home/nspecht/sol", "ZoneVO");
            FileInputStream fis = new FileInputStream(f);
            byte[] tmp = new byte[(int) f.length()];
            fis.read(tmp);
            fis.close();

            PreferedAMFDeserializer amf = new PreferedAMFDeserializer();
            amf.add(tmp, tmp.length);
            amf.readMessage();

            /*
            f = new File("/home/nspecht/Desktop", "mani.txt");
            f.createNewFile();
            FileOutputStream fos = new FileOutputStream(f);
            amf.serialize(fos);
            fos.close();
             */
        } catch (Exception e) {
            DebugLog.put(this, e);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    public void updateGUI() {
        this.jButton1.setText(this.proxy.connected ? "Disconnect" : "Start");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox10;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JCheckBox jCheckBox7;
    private javax.swing.JCheckBox jCheckBox8;
    private javax.swing.JCheckBox jCheckBox9;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
